<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_model extends CI_Model {
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database(); 
    }
    
	function show_all_admin($num,$offset){
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$query=$this->db->get('tbadmin',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	
	function check_admin_user($username){
		$this->load->database();
		$this->db->where(array('user'=>$username));
		$query=$this->db->get('tbadmin');
		if ($query->num_rows() > 0)
		{
		foreach($query->result() as $row){
				return $row->level; 
			}
		}
		return $query->result();
	}
  
	function add_useradmin($username,$pass,$level){ 
		$this->load->database();
    	$data = array( 
            'user'=>$username,
            'pass'=>$pass,
    		'level'=>$level, 
    		'createddate'=>date("d-m-Y h:i:s")
            );
    	$this->db->insert('tbadmin', $data); 
	}
	
	function edit_useradmin($id,$password,$level){
		$this->load->database();
		$data = array(
				'pass'=>$password, 
				'level'=>$level
		);
		$this->db->where('id', $id);
		$this->db->update('tbadmin', $data);
	}
	 
    function checkuser($username,$password){
		
		$this->db->where(array('user'=>$username,'pass'=>$password));
		$query = $this->db->get('tbadmin');
		if($query->num_rows() > 0){
			foreach($query->result() as $row){
				return $row->id; 
			}
		}else{
			return null;
		}
	}
	
	function getDetailsAdmin($id){
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('tbadmin');
		if($query->num_rows() > 0){
			return $query->result();
		}else{
			return null;
		}
	}
	
	function showaccount($userid){
		$this->load->database(); 
		$this->db->where('id',$userid);
		$query = $this->db->get('tbuser');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
	}
	
	function searchuseradmin($username){
		$this->load->database(); 
		$this->db->where('user',$username);
		$query = $this->db->get('tbadmin');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
	}
	
	
	function show_user($num,$offset){
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$query=$this->db->get('tbuser',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	function totaluser(){  
    	$this->load->database();
		return $this->db->count_all('tbuser'); 
	}
	
	function total_admin(){  
    	$this->load->database();
		return $this->db->count_all('tbadmin'); 
	}
	
	function deladmin($id){ 
		$this->load->database();
		$this->db->where('id', $id);
		$this->db->delete('tbadmin'); 
	}
}
    ?>